home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13119 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  49 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: mixing static and const qualifiers for class members
  5. Date: 23 Mar 1996 18:06:07 GMT
  6. Organization: Borland International
  7. Message-ID: <4j1eif$dpc@druid.borland.com>
  8. References: <1996Mar20.143749.90333@ucl.ac.uk> <4j1327$235@coranto.ucs.mun.ca>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4j1327$235@coranto.ucs.mun.ca>, saustin@terra.nlnet.nf.ca says...
  15. >
  16. >ucecb09@ucl.ac.uk (Robert Byrne) wrote:
  17. >
  18. >>The simple way to declare integral class constants is with an enumeration.
  19. >>
  20. >>class Foo{
  21. >>       enum constants{nPositions = 6};
  22. >>       public:
  23. >>       int nData[nPositions];
  24. >>};
  25. >
  26. >I know some compilers ( eg Borland ) allow this, but is this portable?
  27. >I've always cast to int in this situation:
  28. >
  29. >        nt nData[int(nPositions)];
  30. >
  31. >Mind you, I've never checked on this. Am I wasting my time?
  32. >
  33. > - Steve
  34. >
  35.  
  36. The cast is not necessary. Better yet, though, the enum hack is no longer 
  37. necessary. You can initialize a static const int in your class declaration:
  38.  
  39.     class Foo{
  40.            static const nPositions = 6;
  41.            public:
  42.            int nData[nPositions];
  43.     };
  44.  
  45.     const Foo::nPositions;
  46.  
  47.     -- Pete
  48.  
  49.